package com.imay.live.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import com.imay.live.IMApplication;
import com.imay.live.utils.TimeUtil;

/**
 * Created by jeaman on 2017/3/31.
 */

public class PreferenceHelper {
    public static final String PREFERENCE_NAME = "config";
    private static SharedPreferences mSharedPreferences;
    private static PreferenceHelper instance;
    private static SharedPreferences.Editor editor;

    private static final String SHARED_KEY_LOGIN_TOKEN = "LOGIN_TOKEN";    //登录TOKEN
    private static final String SHARED_KEY_BEAUTY_LEVEL = "beauty_level";//美颜等级
    private static final String SHARED_KEY_WHITENING_LEVEL = "whitening_level"; // 美白等级
    private static final String SHARED_KEY_RUDDY_LEVEL = "ruddy_level"; // 红润等级
    private static final String SHARED_KEY_ROOM_GUIDE = "room_guide";   //房间新手指引
    private static final String SHARED_KEY_ROOM_GUIDE_FLOOR = "room_guide_floor";   //房间建楼卡指引
    private static final String SHARED_KEY_ROOM_GUIDE_DIAMON = "room_guide_diamon";   //房间星钻指引
    private static final String SHARED_KEY_API_DOMAIN = "api_domain";   //api服务器（测试用）
    private static final String SHARED_KEY_SOCKET_DOMAIN = "socket_domain";   //socket服务器（测试用）

    private static final String SHARED_KEY_ROOM_NOTICE = "room_notice";   //房间公告

    public static final String CHARGE_ID = "chargeid";//选择的产品ID
    public static final String CHARGE_MODE = "chargemode";//选择的充值方式
    public static final String COVER_URL = "cover_url"; // 封面女郎
    public static final String SEARCHHIST = "searchhistory"; // 搜索历史
    public static final String COCOSVERSONE = "cocosversion"; // cocos 资源版本
    public static final String COCOSVCORE = "cocoscore"; // cocos 版本

    private static final String LOGINPHONECOUNTRY = "loginphonecountryInt";
    private static final String LOGINPHONE = "login_phone";
    public static final String SECRET = "login_sec";
    public static final String ROOMHOUSE_TIME = "roomhouse_time";

    private static final String CHALLENGE = "challenge";

    private static final String APPACTIVATION = "appactivation";

    private static final String TAG_TODAY = "tag_today";//保存当前时间年月日
    private static final String TAG_SHOW_AD_ID = "tag_once_day_ad_list";//保存已经显示的广告id(每天显示的广告)
    private static final String TAG_AD_LIST = "tag_ad_list";//保存已经显示的广告id(只显示一次的广告)

    private PreferenceHelper(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static PreferenceHelper getInstance() {
        if (instance == null) {
            synchronized (PreferenceHelper.class) {
                if (instance == null) {
                    instance = new PreferenceHelper(IMApplication.getInstance());
                }
            }
        }
        return instance;
    }

    public void removeKey(String key) {
        editor.remove(key);
        editor.commit();
    }

    public void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public String getString(String key) {
        return mSharedPreferences.getString(key, null);
    }

    public String getString(String key, @Nullable String defValue) {
        return mSharedPreferences.getString(key, defValue);
    }

    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    public boolean getBoolean(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    public void putLong(String key, long value) {
        editor.putLong(key, value);
        editor.commit();
    }

    public long getLong(String key) {
        return mSharedPreferences.getLong(key, 0L);
    }

    public void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public int getInt(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    public int getInt(String key, int defValue) {
        return mSharedPreferences.getInt(key, defValue);
    }

    //token
    public void saveUserToken(String token) {
        putString(SHARED_KEY_LOGIN_TOKEN, token);
    }

    public void cleanUserToken() {
        removeKey(SHARED_KEY_LOGIN_TOKEN);
    }

    public String getUserToken() {
        return getString(SHARED_KEY_LOGIN_TOKEN);
    }

    public void saveBeautyLevel(int beautyLevel) {
        putInt(SHARED_KEY_BEAUTY_LEVEL, beautyLevel);
    }

    public void saveWhiteningLevel(int whiteningLevel) {
        putInt(SHARED_KEY_WHITENING_LEVEL, whiteningLevel);
    }

    public void saveRuddyLevel(int ruddyLevel) {
        putInt(SHARED_KEY_RUDDY_LEVEL, ruddyLevel);
    }


    public int getBeautyLevel(int defValue) {
        return getInt(SHARED_KEY_BEAUTY_LEVEL, defValue);
    }

    public int getWhiteningLevel(int defValue) {
        return getInt(SHARED_KEY_WHITENING_LEVEL, defValue);
    }

    public int getRuddyLevel(int defValue) {
        return getInt(SHARED_KEY_RUDDY_LEVEL, defValue);
    }

    public boolean getRoomGuide() {
        return getBoolean(SHARED_KEY_ROOM_GUIDE);
    }

    public void saveRoomGuide() {
        putBoolean(SHARED_KEY_ROOM_GUIDE, true);
    }

    public void saveUserInf(int local, String finalPhone, String secret) {
        putInt(LOGINPHONECOUNTRY, local);
        putString(LOGINPHONE, finalPhone);
        putString(SECRET, secret);
    }

    public void savePhone(String finalPhone) {
        putString(LOGINPHONE, finalPhone);
    }

    public int getPhoneCountry() {
        return getInt(LOGINPHONECOUNTRY, -1);
    }

    public String getPhone() {
        return getString(LOGINPHONE);
    }

    public String getSec() {
        return getString(SECRET);
    }

    public void clearSec() {
        putString(SECRET, "");
    }

    public void saveApiDomain(String domain) {
        putString(SHARED_KEY_API_DOMAIN, domain);
    }

    public String getApiDomain(String defValue) {
        return getString(SHARED_KEY_API_DOMAIN, defValue);
    }

    public void saveSocketDomain(String domain) {
        putString(SHARED_KEY_SOCKET_DOMAIN, domain);
    }

    public String getSocketDomain(String defValue) {
        return getString(SHARED_KEY_SOCKET_DOMAIN, defValue);
    }

    public void setChallenge() {
        putBoolean(CHALLENGE, true);
    }

    public boolean getChallenge() {
        return getBoolean(CHALLENGE);
    }

    public void saveAppActivation(){
        putBoolean(APPACTIVATION, true);
    }

    public boolean getAppActivation() {
        return getBoolean(APPACTIVATION);
    }

    public boolean getRoomFloorGuide() {
        return getBoolean(SHARED_KEY_ROOM_GUIDE_FLOOR);
    }

    public void saveRoomFloorGuide() {
        putBoolean(SHARED_KEY_ROOM_GUIDE_FLOOR, true);
    }
    public boolean getRoomDiamonGuide() {
        return getBoolean(SHARED_KEY_ROOM_GUIDE_DIAMON);
    }

    public void saveRoomDiamonGuide() {
        putBoolean(SHARED_KEY_ROOM_GUIDE_DIAMON, true);
    }

    public void saveRoomNotice(String notice) {
        putString(SHARED_KEY_ROOM_NOTICE, notice);
    }

    public String getRoomNotice() {
        return getString(SHARED_KEY_ROOM_NOTICE);
    }
    public void saveToday() {
        putString(TAG_TODAY,TimeUtil.getToday());
    }
    public String getSaveDay() {
        return getString(TAG_TODAY);
    }

    public void saveShowAdId(String showAdId) {
        putString(TAG_SHOW_AD_ID,showAdId);
    }
    public String getShowAdId() {
        return getString(TAG_SHOW_AD_ID);
    }
    public void saveAdList(String adListJson) {
        putString(TAG_AD_LIST,adListJson);
    }
    public String getAdList() {
        return getString(TAG_AD_LIST);
    }
}
